//
//  JKIndexMyWelfareRedPacketVC.m
//  chequanbao_ios
//
//  Created by 朱凯 on 2018/1/24.
//  Copyright © 2018年 com.itboye. All rights reserved.
//

#import "JKIndexMyWelfareRedPacketVC.h"

// vc
#import "ByUIWithdrawNewVC.h"


// cell
#import "JKCellRedPactet.h"

// model
#import "ByModelMineConsume.h"
#import "ByModelMinePoints.h"

// view
#import "JKViewFriendsSegment.h"

// api
#import "ByApiMine.h"

@interface JKIndexMyWelfareRedPacketVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) JKViewFriendsSegment *segment;
@property (nonatomic, strong) UILabel *moneyLabel;
@property (nonatomic, strong) UIButton *tixianButton;

@property (nonatomic, assign) NSInteger type;       // 2：收入，1：支出
@property (nonatomic, assign) NSInteger page_index;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation JKIndexMyWelfareRedPacketVC

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"红包明细";
    [[self.view viewWithTag:888] removeFromSuperview];
    
    [self addColorLeftBackBarButtonItem];
    
    [self createViews];
    
    _type = 2;
    
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self setNaviAttributeWithTranslucent:false stateBarLightContent:false];
    
    [self fetchData];
}

#pragma mark - 请求数据

- (void)fetchData{
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    model.uid = BY_USER.uid;
    model.role_type = BY_USER.account_id;
    model.change_type = [NSString stringWithFormat:@"%li", self.type];
    model.page_index = [NSString stringWithFormat:@"%ld",self.page_index];
    model.page_size = @"10";
    
    [ByApiMine changeRecord:model success:^(ByNetBaseResult *result) {
        NSInteger count = [result.data[@"count"] integerValue];
        NSMutableArray *temp = [NSMutableArray array];
        for (ByModelMinePoints *model in result.data[@"list"]) {
            [temp addObject:model];
        }
        if (self.page_index == 1) {
            [self.dataArray removeAllObjects];
            [self.dataArray addObjectsFromArray:[temp copy]];
            [self.table.mj_footer resetNoMoreData];
        }else{
            [self.dataArray addObjectsFromArray:[temp copy]];
        }
        [self.table reloadData];
    
        if (self.dataArray.count == count) {
            [self.table.mj_header endRefreshing];
            [self.table.mj_footer endRefreshingWithNoMoreData];
            return ;
        }
        [self.table.mj_header endRefreshing];
        [self.table.mj_footer endRefreshing];
        
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showOnlyText:result.msg];
    }];
}


#pragma mark - 创建视图
- (void)createViews {
    
    UIImageView *moneyBgView = [UIImageView new];
    moneyBgView.image = [UIImage imageNamed:@"红包-头视图"];
    [self.view addSubview:moneyBgView];
    [moneyBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(@0);
        make.height.equalTo(@(SCREEN_WIDTH / 750 * 186));
    }];
    
    _moneyLabel = [[UILabel alloc] init];
    _moneyLabel.text = [NSString stringWithFormat:@"现有红包 %li￥", BY_USER.points.integerValue / 100];
    _moneyLabel.font = B_FONT(20);
    _moneyLabel.textAlignment = NSTextAlignmentCenter;
    _moneyLabel.textColor = [UIColor whiteColor];
    [moneyBgView addSubview:_moneyLabel];
    [_moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(moneyBgView);
    }];
    
    _segment = [JKViewFriendsSegment new];
    _segment.segments = @[@"我的收入", @"提现记录"];
    [self.view addSubview:_segment];
    [_segment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(moneyBgView.mas_bottom);
        make.height.equalTo(@60);
    }];
    __weak typeof(self) wself = self;
    _segment.selectBlock = ^(NSInteger index) {
        if (index == 0) {
            wself.type = 2;
        } else {
            wself.type = 1;
        }
        [wself.table.mj_header beginRefreshing];
    };
    
    _tixianButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _tixianButton.backgroundColor = rgba(120, 197, 250, 1);
    _tixianButton.titleLabel.font = FONT(14);
    [_tixianButton setTitle:@"提现" forState:UIControlStateNormal];
    [_tixianButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_tixianButton addTarget:self action:@selector(tixian) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_tixianButton];
    [_tixianButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.height.equalTo(@40);
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-0);
        } else {
            make.bottom.equalTo(@0);
        }
    }];
    
    [self createTableView];
}
- (void)createTableView {
    
    self.table = [[UITableView alloc] init];
    self.table.backgroundColor = TABLE_COLOR;
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    [self.table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(0, 0, 0, 0)).priorityMedium();
        make.top.equalTo(_segment.mas_bottom);
        make.bottom.equalTo(_tixianButton.mas_top);
    }];
    
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        _page_index = 1;
        [self fetchData];
    }];
    self.table.mj_header = header;
    
    ByCoreRefreshGifFooter *footer = [ByCoreRefreshGifFooter footerWithRefreshingBlock:^{
        _page_index += 1;
        [self fetchData];
    }];
    self.table.mj_footer = footer;
    
    [self.table registerClass:[JKCellRedPactet class] forCellReuseIdentifier:@"cell"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    JKCellRedPactet *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model = self.dataArray[indexPath.section];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return UIView.new;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return UIView.new;
}

#pragma mark - 事件
// 提现
- (void)tixian {
    ByUIWithdrawNewVC *vc = [[ByUIWithdrawNewVC alloc] init];
    [self by_normalPush:vc hasNavi:YES];
}

#pragma mark - getter
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
@end
